package org.example
import scala.language.postfixOps
import org.apache.spark.sql.types.{DoubleType, IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.{Row, SQLContext, SparkSession}
object Yun_SQL2 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .master("local[*]")
      .appName("sparkBase")
      .getOrCreate()
    val sc = spark.sparkContext
    val lines=sc.textFile("src/main/resources/person.txt")
    val personRDD=lines.map(line=>{
      val fields = line.split(",")
      val id = fields(0).toInt
      val name = fields(1)
      val age = fields(2).toInt
      val height = fields(3).toDouble

      Row(id,name,age,height)
    })
//    import sqlContext.implicits._
//    val df = personRDD.toDF()
    //    df.show()
    val schema = StructType(List(
      StructField("id",IntegerType,true),
      StructField("name",StringType,true),
      StructField("age",IntegerType,true),
      StructField("height",DoubleType,true)
    ))
    val df = spark.createDataFrame(personRDD,schema)
    import spark.implicits._
    val resultDataFrame = df.select("id","name","age","height").orderBy($"age" desc,$"height" desc,$"name" desc)
//    val resultDataFrame1 = df.select("id","name","age","height").where($"age">10)
//      .orderBy($"age" asc,$"height" asc,$"name" asc)
    resultDataFrame.show()
    sc.stop()
  }
}
